<template>
  <div class="config-widget-wrapper">
    <h1 class="title">首页配置模版</h1>

    <div class="content">
      <!-- 左边区域 -->
      <div class="left-area">
        <h3 class="start-tip">开始配置</h3>
        <p class="click-tip">点击组件添加栏目</p>

        <div class="template-tip">轮播图片模版</div>
        <div class="banner" @click="leftTemplateClick('banner')">
          <img
            v-if="selectColumnType === 'banner'"
            class="img-banner"
            src="../../assets/image/icon-banner-select.png"
            alt=""
          />
          <img v-else class="img-banner" src="../../assets/image/icon-banner.png" alt="" />
        </div>

        <div class="template-tip">金刚区模版</div>
        <div class="kong" @click="leftTemplateClick('kong')">
          <img
            class="kong-img"
            v-if="selectColumnType === 'kong'"
            src="../../assets/image/icon-kong-select.png"
            alt=""
          />
          <img class="kong-img" v-else src="../../assets/image/icon-kong.png" alt="" />
        </div>

        <div class="template-tip">标准图片模版</div>
        <div class="image-template-content">
          <div class="content-image" @click="leftTemplateClick('pic-1')">
            <img
              v-if="selectColumnType === 'pic-1'"
              class="image-template-1"
              src="../../assets/image/image-template-1-select.png"
            />
            <img v-else class="image-template-1" src="../../assets/image/image-template-1.png" />
          </div>
          <div class="content-image" @click="leftTemplateClick('pic-2')">
            <img
              v-if="selectColumnType === 'pic-2'"
              class="image-template-2"
              src="../../assets/image/image-template-2-select.png"
            />
            <img v-else class="image-template-2" src="../../assets/image/image-template-2.png" />
          </div>
          <div class="content-image" @click="leftTemplateClick('pic-3')">
            <img
              v-if="selectColumnType === 'pic-3'"
              class="image-template-3"
              src="../../assets/image/image-template-3-select.png"
            />
            <img v-else class="image-template-3" src="../../assets/image/image-template-3.png" />
          </div>
          <div class="content-image" @click="leftTemplateClick('pic-4')">
            <img
              v-if="selectColumnType === 'pic-4'"
              class="image-template-4"
              src="../../assets/image/image-template-4-select.png"
            />
            <img v-else class="image-template-4" src="../../assets/image/image-template-4.png" />
          </div>
        </div>
      </div>
      <!-- 中间手机区域 -->
      <div class="center-area">
        <div class="center-content">
          <H5Designer
            :attr="props.configData"
            :addColumnContent="addColumnContent"
            :editColumn="editColumn"
            :deleteColumn="deleteColumn"
            :modifyColumnContent="updateColumnContent"
          ></H5Designer>
        </div>
      </div>
      <!-- 右边区域 -->
      <div class="right-area">
        <div class="top_box" v-show="selectColumnType">
          <div class="top">
            <span class="setting-tip">{{ selectColumnType }}</span>
            <span class="expand" @click="expandClick()">{{ isExpand ? '收起' : '展开' }}</span>
          </div>
          <el-form ref="ruleFormRef" :model="form" :rules="rules" v-show="isExpand" class="form-content">
            <el-form-item label="栏目名称：" prop="columnName">
              <el-input v-model="form.columnName" placeholder="请输入栏目名称" />
            </el-form-item>
            <el-form-item label="栏目类型：" prop="columnStyle">
              <el-select v-model="form.columnStyle" placeholder="请选择栏目类型">
                <el-option label="金刚区" :value="1" />
                <el-option label="轮播图" :value="2" />
                <el-option label="列表2" :value="6" />
                <el-option label="列表4" :value="7" />
              </el-select>
            </el-form-item>
            <el-form-item label="栏目权重：" prop="weight">
              <el-input v-model="form.weight" placeholder="请输入权重" />
            </el-form-item>
            <el-form-item label="显示个数：" prop="elementCount">
              <el-input v-model="form.elementCount" placeholder="请输入内容显示个数" />
            </el-form-item>

            <el-form-item label="更多设置：" prop="itemMore">
              <el-select v-model="form.itemMore" placeholder="请选择更多设置">
                <el-option label="不显示" :value="0" />
                <el-option label="显示当前栏目内容" :value="1" />
                <el-option label="显示指定内容" :value="2" />
              </el-select>
            </el-form-item>
            <el-form-item label="更多列表：" prop="itemList" v-if="form.itemMore == 2">
              <el-select v-model="form.itemList" placeholder="请选择更多列表">
                <el-option label="不跳转" :value="0" />
                <el-option label="H5页面" :value="1" />
                <el-option label="课程列表" :value="2" />
                <el-option label="讲师列表" :value="3" />
                <el-option label="游戏列表" :value="4" />
                <el-option label="游戏" :value="5" />
              </el-select>
            </el-form-item>
            <el-form-item label="跳转链接：" prop="itemJuup" v-if="form.itemList == 1">
              <el-input v-model="form.itemJuup" placeholder="请输入跳转链接" />
            </el-form-item>
            <el-form-item label="课程标签：" prop="itemLabel" v-if="form.itemList == 2">
              <el-input v-model="form.itemLabel" placeholder="请输入课程标签" />
            </el-form-item>
            <el-form-item class="submit-button">
              <el-button type="primary" @click="onSubmit(ruleFormRef)">确定</el-button>
            </el-form-item>
          </el-form>
        </div>

        <!-- 图片上传form -->
        <div class="bott_box" v-show="selectColumnContentType">
          <div class="bott">
            <!-- <span class="setting-tip">111111</span> -->
            <span class="setting-tip">{{ selectColumnContentType }}</span>
            <span class="expand" @click="expandClickOne()">{{ isExpandOne ? '收起' : '展开' }}</span>
          </div>

          <div class="box_content" v-show="isExpandOne">
            <el-form class="form-content" ref="ruleImgFormRef" :model="imgForm" :rules="imgFormRules">
              <el-form-item label="上传图片" prop="itemUpload">
                <el-upload
                  class="avatar-uploader"
                  action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload"
                >
                  <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                  <!-- <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon> -->
                </el-upload>
                <!-- <el-input v-model="imgForm.itemJuup" placeholder="请上传图片" /> -->
              </el-form-item>
              <el-form-item label="跳转方式：" prop="itemList">
                <el-select v-model="imgForm.itemList" placeholder="请选择跳转方式">
                  <el-option label="不跳转" :value="0" />
                  <el-option label="H5页面" :value="1" />
                  <el-option label="课程列表" :value="2" />
                  <el-option label="讲师列表" :value="3" />
                  <el-option label="游戏列表" :value="4" />
                  <el-option label="游戏" :value="5" />
                </el-select>
              </el-form-item>
              <el-form-item label="跳转链接：" prop="itemJuup" v-if="imgForm.itemList == 1">
                <el-input v-model="imgForm.itemJuup" placeholder="请输入跳转链接" />
              </el-form-item>
              <el-form-item label="课程标签：" prop="itemLabel" v-if="imgForm.itemList == 2">
                <el-input v-model="imgForm.itemLabel" placeholder="请输入课程标签" />
              </el-form-item>
              <el-form-item label="标题名称：" prop="itemName">
                <el-input v-model="imgForm.itemName" placeholder="请输入标题名称" />
              </el-form-item>
              <el-form-item label="内容权重：" prop="itemWeight">
                <el-input v-model="imgForm.itemWeight" placeholder="请输入内容权重" />
              </el-form-item>

              <el-form-item class="submit-button">
                <el-button type="primary" @click="onSubmitTwo(ruleImgFormRef)">确定</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, toRaw } from 'vue';
import { ElFormItem, ElInput, ElButton, ElSelect, ElOption, ElForm, ElMessage, ElMessageBox } from 'element-plus';
import type { FormInstance, FormRules } from 'element-plus';
import { HttpClient } from '@kunlun/dependencies';
import H5Designer from './H5Designer.vue';

const http = HttpClient.getInstance();
const props = defineProps({
  configData: {
    type: [],
    default: []
  },
  createColumn: {
    type: Function
  },
  modifyColumn: {
    type: Function
  },
  deleteColumn: {
    type: Function
  },
  createColumnContent: {
    type: Function
  },
  modifyColumnContent: {
    type: Function
  }
});

// const imageUrl = ref('');
const imageUrl = ref(
  'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
);
const ruleFormRef = ref();
const ruleImgFormRef = ref(); // 上传图片form
// 是否是创建栏目
const isCreateColumn = ref(true);
// 是否是创建栏目内容
const isCreateColumnContent = ref(true);
// 选择的栏目模版类型
const selectColumnType = ref('');
// 选择栏目内容类型
const selectColumnContentType = ref('');

const leftTemplateClick = (param) => {
  if (selectColumnType.value && selectColumnType.value !== param) {
    open(param);
    return;
  }
  selectColumnType.value = param;
  isCreateColumn.value = true;
  selectColumnContentType.value = '';

  ruleFormRef.value.resetFields();
};

const open = (param) => {
  ElMessageBox.confirm('切换内容方式会清空您当前内容下配置的数据，确认继续吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
    customClass: 'oe-dialog-btn',
    center: true
  })
    .then(() => {
      selectColumnType.value = param;
      isCreateColumn.value = true;
      selectColumnContentType.value = '';

      ruleFormRef.value.resetFields();
    })
    .catch(() => {});
};

const rules = reactive({
  columnName: [{ required: true, message: '请输入栏目名称', trigger: 'change' }],
  weight: [
    {
      required: true,
      message: '请输入权重',
      trigger: 'change'
    }
  ],
  elementCount: [
    {
      required: true,
      message: '请输入内容显示个数',
      trigger: 'change'
    }
  ],
  columnStyle: [
    {
      required: true,
      message: '请选择栏目类型',
      trigger: 'change'
    }
  ]
});

const form = reactive({
  columnName: '', // 栏目名称
  columnStyle: '', // 栏目类型
  weight: '', // 栏目权重
  elementCount: '', // 显示个数
  itemMore: '', // 更多列表
  itemJuup: '', // 跳转链接
  itemLabel: '', // 课程标签
  itemList: '' // 更多设置
});

// img上传图片form规则
const imgFormRules = reactive({
  itemName: [{ required: true, message: '请输入标题名称', trigger: 'change' }],
  itemWeight: [
    {
      required: true,
      message: '请录入权重',
      trigger: 'change'
    }
  ],
  itemJuup: [
    {
      required: true,
      message: '请输入跳转链接',
      trigger: 'change'
    }
  ],
  itemLabel: [
    {
      required: true,
      message: '请输入课程标签',
      trigger: 'change'
    }
  ],
  itemList: [
    {
      required: true,
      message: '请选择跳转方式',
      trigger: 'change'
    }
  ]
});

// img上传图片formdata
const imgForm = reactive({
  itemList: '', // 跳转方式
  itemName: '', // 标题名称
  itemWeight: '', // 内容权重
  itemJuup: '', // 跳转链接
  itemLabel: '', // 课程标签
  itemUpload: '' // 上传图片
});
const onSubmit = (formEl) => {
  if (!formEl) {
    return;
  }
  formEl.validate((valid, fields) => {
    if (valid) {
      if (isCreateColumn.value) {
        props.createColumn(toRaw(form));
      } else {
        props.modifyColumn(toRaw(form));
      }
    } else {
      console.log('error submit!', fields);
    }
  });
};

const onSubmitTwo = (formEl) => {
  if (!formEl) {
    return;
  }
  formEl.validate((valid, fields) => {
    if (valid) {
      if (isCreateColumnContent.value) {
        props.createColumnContent(toRaw(imgForm));
      } else {
        props.modifyColumnContent(toRaw(imgForm));
      }
    } else {
      console.log('error submit!', fields);
    }
  });
};
// 上部展开收起
const isExpand = ref(true);
const expandClick = () => {
  isExpand.value = !isExpand.value;
};
// 下部展开收起
const isExpandOne = ref(true);
const expandClickOne = () => {
  isExpandOne.value = !isExpandOne.value;
};
// 添加栏目内容
const addColumnContent = (item) => {
  console.log('HomeConfig 添加栏目内容', item);
  isCreateColumnContent.value = true;
  selectColumnContentType.value = item.columnName;
};

// 修改栏目内容
const modifyColumnContent = (item) => {
  console.log('HomeConfig 修改栏目内容', item);
  isCreateColumnContent.value = false;
  selectColumnContentType.value = item.columnName;
  imgForm.itemList = item.itemList;
  imgForm.itemName = item.columnName;
  imgForm.itemWeight = item.weight;
};

// 添加或者更新栏目内容
const updateColumnContent = (item) => {
  console.log('HomeConfig  添加或者更新栏目内容', item);
  if (item.id) {
    // 修改栏目内容
    modifyColumnContent(item);
  } else {
    // 增加栏目内容
    addColumnContent(item);
  }
};

// 修改栏目
const editColumn = (item) => {
  console.log('HomeConfig  修改栏目');
  isCreateColumn.value = false;
  selectColumnType.value = item.columnName;
  selectColumnContentType.value = '';
  form.columnName = item.columnName;
  form.weight = item.weight;
  form.elementCount = item.elementCount;
  form.itemMore = item.itemMore;
  form.itemJuup = item.itemJuup;
  form.itemLabel = item.itemLabel;
  form.itemList = item.itemList;
};

// 删除栏目
const deleteColumn = (item) => {
  console.log('HomeConfig  删除栏目');
  props.deleteColumn([{ id: item.id }]);
};

// 上传图片
const handleAvatarSuccess = (res, file) => {
  console.log(res, file);
  imageUrl.value = URL.createObjectURL(file.raw);
};

// 上传图片之前
const beforeAvatarUpload = (file) => {
  console.log(file);
  const isJPG = file.type === 'image/jpeg';
  const isLt2M = file.size / 1024 / 1024 < 2;

  if (!isJPG) {
    ElMessage.error('上传头像图片只能是 JPG 格式!');
  }
  if (!isLt2M) {
    ElMessage.error('上传头像图片大小不能超过 2MB!');
  }
  return isJPG && isLt2M;
};
</script>

<style lang="scss" scoped>
.config-widget-wrapper {
  // --el-border-radius-base: 20px;
  // --el-color-primary-light-3: #ffcd12;
  // --el-button-hover-bg-color: #ffcd12 !important;
  // --el-button-hover-border-color: #ffcd12;
  // --el-button-active-color: #ffcd12;

  width: 1280px;
  height: 100vh;

  padding: 20px;

  .title {
    font-size: 18px;
    height: 45px;
    line-height: 45px;
    color: rgba(0, 0, 0, 0.85);
  }

  .content {
    display: flex;
    align-items: flex-start;
    color: rgba(0, 0, 0, 0.6);

    .left-area {
      width: 250px;
      height: 603px;
      background-color: #fff;
      border-radius: 10px;
      padding: 20px;

      .start-tip {
        font-size: 14px;
      }

      .click-tip {
        font-size: 12px;
        margin-top: 10px;
      }

      .template-tip {
        border-top: 1px solid rgba(0, 0, 0, 0.07);
        font-weight: 700;
        font-size: 12px;
        padding: 20px 0;
        margin-top: 20px;
      }

      .img-banner {
        width: 90px;
        height: 43px;

        &:hover {
          content: url('../../assets/image/icon-banner-select.png');
        }
      }

      .kong-img {
        width: 140px;

        &:hover {
          content: url('../../assets/image/icon-kong-select.png');
        }
      }

      .image-template-content {
        display: flex;
        flex-wrap: wrap;

        .content-image {
          width: 102px;
          height: 75px;

          .image-template-1 {
            width: 66px;
            height: 42px;

            &:hover {
              content: url('../../assets/image/image-template-1-select.png');
            }
          }

          .image-template-2 {
            width: 53px;
            height: 54px;

            &:hover {
              content: url('../../assets/image/image-template-2-select.png');
            }
          }

          .image-template-3 {
            width: 48px;
            height: 55px;

            &:hover {
              content: url('../../assets/image/image-template-3-select.png');
            }
          }

          .image-template-4 {
            width: 51px;
            height: 54px;

            &:hover {
              content: url('../../assets/image/image-template-4-select.png');
            }
          }
        }
      }
    }

    .center-area {
      display: flex;
      width: 420px;
      height: 760px;
      background-size: 325px auto;
      background-repeat: no-repeat;
      background-image: url('../../assets/image/icon_model_bg.png');
      margin: 0 50px;
      background-color: #f8f8f8;
      padding-top: 50px;
      padding-left: 17px;
      .center-content {
        height: 580px;
        overflow-y: auto;
      }
    }

    .right-area {
      width: 323px;
      height: auto;
      .top_box {
        height: auto;
        background-color: #fff;
        border-radius: 10px;
        padding: 20px;
        .top {
          display: flex;
          justify-content: space-between;
          color: rgba(0, 0, 0, 0.85);
          align-items: center;
          padding-bottom: 15px;
          border-bottom: 1px solid rgba(0, 0, 0, 0.03);

          .setting-tip {
            font-size: 14px;
          }

          .expand {
            font-size: 12px;
          }
        }

        .form-content {
          margin-top: 25px;

          .submit-button {
            display: flex;
            justify-content: center;
          }
        }

        ::v-deep .el-form-item__label {
          align-items: center;

          &::before {
            display: inline-block;
            margin-right: 4px;
            color: #ff4d4f;
            font-size: 14px;
            font-family: SimSun, sans-serif;
            line-height: 1;
            content: '*';
          }
        }

        ::v-deep .el-input {
          width: 190px;
        }

        ::v-deep .el-button > span {
          color: #000;
        }
      }
      .bott_box {
        margin-top: 20px;
        background-color: #fff;
        border-radius: 10px;
        padding: 20px;
        height: auto;
        .bott {
          display: flex;
          justify-content: space-between;
          color: rgba(0, 0, 0, 0.85);
          align-items: center;
          padding-bottom: 15px;
          border-bottom: 1px solid rgba(0, 0, 0, 0.03);

          .setting-tip {
            font-size: 14px;
          }

          .expand {
            font-size: 12px;
          }
        }

        .box_content {
          margin-top: 25px;
          .submit-button {
            // display: flex;
            // justify-content: center;
            ::v-deep .el-form-item__content {
              display: flex;
              justify-content: center;
            }
          }
          ::v-deep .el-button > span {
            color: #000;
          }

          .avatar {
            width: 100%;
            height: 150px;
          }
        }
      }
    }

    // ::v-deep .el-button:focus,
    // .el-button:hover {
    //   border-color: #ffcd12 !important;
    //   background-color: #ffcd12 !important;
    // }
  }
}
</style>
